import java.util.*;

/**
 * @author LKQ
 * @date 2022/3/13 9:30
 * @description
 */
public class FormalSolution {
    public static void main(String[] args) {
        FormalSolution formalSolution = new FormalSolution();
        int[] data = {235, 140, 4};
        formalSolution.validUtf8(data);
    }
    public boolean validUtf8(int[] data) {
        int n = data.length;
        for (int i = 0; i < n; ) {
            int t = data[i], j = 7;
            while (j >= 0 && (((t >> j) & 1) == 1)) {
                j--;
            }
            int cnt = 7 - j;
            if (cnt == 1 || cnt > 4) {
                return false;
            }
            if (i + cnt - 1 >= n) {
                return false;
            }
            for (int k = i+1; k < i + cnt; k++) {
                t = data[k];
                if (((( t >> 7) & 1) == 1) && ((( t >> 6) & 1) == 0)) {
                    continue;
                }
                return false;
            }
            if (cnt == 0) {
                i++;
            }else {
                i += cnt;
            }
        }
        return true;
    }
}
